﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxTreeList;
using System.Data;
using System.Web;

public class BasePage : System.Web.UI.Page
{
    protected string _company = System.Configuration.ConfigurationManager.AppSettings["TitlePage"].ToString();

    private ASPxGridView _baseGv;
    public ASPxGridView baseGv
    {
        get { return _baseGv; }
        set
        {
            _baseGv = value;
        }
    }
    private ASPxTreeList _basetreelist;
    public ASPxTreeList basetreelist
    {
        get { return _basetreelist; }
        set
        {
            _basetreelist = value;
        }
    }
    private bool _isSetPageSize = true;
    public bool isSetPageSize
    {
        get { return _isSetPageSize; }
        set
        {
            _isSetPageSize = value;
        }
    }
    public BasePage()
    {



    }
    //protected override void OnInit(EventArgs e)
    //{
    //    base.OnInit(e);
    //    if (Context.Session != null && Session.IsNewSession)
    //    {
    //        string szCookieHeader = Request.Headers["Cookie"];
    //        if ((szCookieHeader != null) && (szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
    //        {
    //            if (IsPostBack)
    //                Response.RedirectLocation = ResolveUrl("~/Default.aspx?returnURL=" + Request.RawUrl);
    //            else
    //                Response.Redirect("~/Default.aspx?returnURL=" + Request.RawUrl);

    //        }
    //    }
    //}
    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);
        this.Theme = "Custom";
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);

        if (!IsPostBack)
        {

        }
        //if (IsCallback)
        //{
        //    if (Session["BOID"] == null || Session["userid"] == null)
        //    {
        //        Response.RedirectLocation = "Default.aspx";
        //    }
        //}
    }

    protected Control getGridViewTemplateControl(string ControlName)
    {
        return baseGv.FindEditFormTemplateControl(ControlName);
    }

    public void gridCustomJSProperties(ASPxGridView gv, ASPxGridViewClientJSPropertiesEventArgs e)
    {
        e.Properties["cpPageSize"] = gv.SettingsPager.PageSize;
    }

    public void gridCallBackOption(ASPxGridView gv, string param, string KeyFieldName, SqlDataSource ds)
    {
        switch (param)
        {
            case "Reload":
                gv.DataBind();
                break;
            case "10":
                gv.SettingsPager.PageSize = 10;
                break;
            case "20":
                gv.SettingsPager.PageSize = 20;
                break;
            case "25":
                gv.SettingsPager.PageSize = 25;
                break;
            case "all":
                gv.Settings.VerticalScrollBarMode = DevExpress.Web.ASPxClasses.ScrollBarMode.Auto;
                gv.SettingsPager.Mode = GridViewPagerMode.ShowAllRecords;
                break;
            case "isShowFiltertrue":
                gv.Settings.ShowFilterBar = GridViewStatusBarMode.Visible;
                break;
            case "isShowFilterfalse":
                gv.Settings.ShowFilterBar = GridViewStatusBarMode.Hidden;
                break;
            case "isShowHScrolltrue":
                gv.Settings.HorizontalScrollBarMode = DevExpress.Web.ASPxClasses.ScrollBarMode.Auto;
                break;
            case "isShowHScrollfalse":
                gv.Settings.HorizontalScrollBarMode = DevExpress.Web.ASPxClasses.ScrollBarMode.Hidden;
                break;
            case "isShowHypColtrue":
                gv.AccessibilityCompliant = true;
                break;
            case "isShowHypColfalse":
                gv.AccessibilityCompliant = false;
                break;
            case "isShowGrouptrue":
                gv.Settings.ShowGroupPanel = true;
                break;
            case "isShowGroupfalse":
                gv.Settings.ShowGroupPanel = false;
                break;
            case "delselect":
                List<object> keyValues = gv.GetSelectedFieldValues(KeyFieldName);

                for (int i = 0; i < keyValues.Count; i++)
                {
                    ds.DeleteParameters.Clear();
                    ds.DeleteParameters.Add(KeyFieldName, keyValues[i].ToString());
                    ds.Delete();
                }
                break;

            default:
                //gv.SettingsPager.PageSize = 10;
                break;
        }

        if (param.IndexOf("globalFilter") >= 0)
        {
            string[] listparams = param.Split(';');
            string exp = "";
            int loop = 0;
            string keys = listparams[2];
            if (keys.Length > 0)
            {
                foreach (GridViewColumn col in gv.VisibleColumns)
                {
                    if (col.Visible && col.Name != "#" && (col is GridViewDataTextColumn || col is GridViewDataColumn || col is GridViewDataMemoColumn))
                    {
                        if (loop > 0)
                        {
                            exp += " OR ";
                        }

                        switch (listparams[1].Trim())
                        {
                            case "1":
                                {
                                    exp += "[" + (col as GridViewDataColumn).FieldName + "] LIKE '%" + keys + "%'";
                                }
                                break;
                            case "0":
                                {
                                    string[] key = new string[1];
                                    if (keys.IndexOf(" ") > 0)
                                    {
                                        key = new string[keys.LastIndexOf(" ") + 1];
                                        key = keys.Split(' ');
                                    }
                                    else
                                        key[0] = keys;

                                    for (int i = 0; i < key.Length; i++)
                                    {
                                        if (key[i].Trim().Length > 0)
                                        {
                                            if (i > 0)
                                                exp += " OR ";
                                            exp += "[" + (col as GridViewDataColumn).FieldName + "] LIKE '%" + key[i] + "%'";
                                        }
                                    }

                                }
                                break;

                            default:
                                break;
                        }
                        loop++;
                    }
                }
            }
            gv.FilterExpression = exp;
        }
        gv.Selection.UnselectAll();
        gv.DataBind();
    }

    protected void GenerateScript(string script)
    {
        Page.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "<script> " + script + " </script>");
    }
}
